<!--
* description: 合作伙伴管理
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="false"
      :dynamicIsColumnDrop="true"
    >
      <template slot="controlBtn" slot-scope="scope">
        <el-link
          type="primary"
          icon="el-icon-edit"
          :underline="false"
          @click="edit(scope.row.index)"
          title="编辑"
          style="font-size:12px;"
        >编辑</el-link>
        <el-link
          type="primary"
          icon="el-icon-printer"
          :underline="false"
          title="箱号打印"
          style="font-size:12px;"
          @click="printNum(scope.row)"
        >箱号打印</el-link>
      </template>
    </one-table-template>
    <edit
      :dynamicEditRowData="editRowData"
      :popupsVisible="editPopupsVisible"
      :key="editPopupsKey"
      :popupsState="editPopupsState"
      @close="close"
    ></edit>
    <printNum v-if="isShow" :rowData="rowData" @detailClose="detailClose" :isShow="isShow" />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { wmsApis } from "@/api/wms/baseData/api";
import OneTableTemplate from "@/components/wms/templates/oneTable";
// import httpRequestExport from "@utils/wms/export"
import request from "@/utils/request";
export default {
  name: "partnerManage",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate,
    Edit: () => import("./edit"),
    printNum: () => import("./printNum")
  },
  watch: {
    $route(val) {
      if (
        this.formField.pageId !== "" &&
        this.$route.query.pageId !== undefined &&
        this.$route.query.pageId !== this.formField.pageId
      ) {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
        this.queryTable();
      }
    }
  },
  created() {
    if (this.$route.query != null) {
      // 判断传入query参数，初始化数据
      if (this.$route.query.pageId != null && this.$route.query.pageId !== "") {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
      }
    }
  },
  mounted() {
    this.queryTable();
    this.querySelect();
  },
  data() {
    return {
      // 网格查询API配置对象
      apiConfig: wmsApis.queryPartner,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询" //"查询"
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          clickEvent: () => this.add(),
          name: "add",
          position: "left",
          text: "新增" //"新增"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置" //"重置"
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          clickEvent: () => this.export(),
          name: "export",
          position: "left",
          text: "导出" //"导出"
        },
        {
          compKey: "btnKey6",
          type: "",
          size: "small",
          name: "importTemplateDownload",
          position: "left",
          clickEvent: () => this.downloadTemplate(),
          text: "模板下载" //"模板下载"
        },
        {
          compKey: "btnKey5",
          text: "导入",
          category: "ve",
          codeField: "fileChannel",
          position: "upload",
          isMust: true
        } //导入
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "合作伙伴类别", //合作伙伴类别
          codeField: "PARTNER_TYPE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: []
        },
        {
          compKey: "compKey2",
          labelName: "是否入库来源", //是否入库来源
          codeField: "IS_IN",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: [
            {
              label: "是",
              value: 1
            },
            {
              label: "否",
              value: 0
            }
          ]
        },
        {
          compKey: "compKey3",
          labelName: "是否出库去向", //是否出库去向
          codeField: "IS_OUT",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: [
            {
              label: "是",
              value: 1
            },
            {
              label: "否",
              value: 0
            }
          ]
        },
        {
          compKey: "compKey4",
          labelName: "状态", // '状态',
          codeField: "IS_ENABLE",
          component: () => import("@/components/wms/isEnable/isEnableOne"),
          type: "dropdownList",
          isMust: true
        },
        {
          compKey: "compKey5",
          labelName: "合作伙伴编号", //合作伙伴编号
          codeField: "PARTNER_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey6",
          labelName: "合作伙伴简称", //合作伙伴简称
          codeField: "PARTNER_SHORT_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "controlBtn",
          label: "操作", //"操作"
          codeField: "controlBtn",
          width: 140,
          align: "center",
          isSlot: true
        },
        //合作伙伴类别
        {
          prop: "PARTNER_TYPE_NAME",
          label: "合作伙伴类别",
          align: "center"
        },
        //合作伙伴编号
        {
          prop: "PARTNER_CODE",
          label: "合作伙伴编号",
          align: "center"
        },
        //合作伙伴简称
        {
          prop: "PARTNER_SHORT_NAME",
          label: "合作伙伴简称",
          align: "center"
        },
        //合作伙伴全称
        {
          prop: "PARTNER_FULL_NAME",
          label: "合作伙伴全称",
          align: "center"
        },
        //是否入库来源
        {
          prop: "IS_IN_STR",
          label: "是否入库来源",
          align: "center"
        },
        //是否出库去向
        {
          prop: "IS_OUT_STR",
          label: "是否出库去向",
          align: "center"
        },
        //状态
        {
          prop: "IS_ENABLE_STR",
          label: "状态",
          align: "center"
        },
        //创建时间
        {
          prop: "CREATED_DATE_NEW",
          label: "创建时间",
          align: "center"
        },
        //修改时间
        {
          prop: "LAST_UPDATED_DATE_NEW",
          label: "修改时间",
          align: "center"
        }
      ],
      //表单查询数据
      formField: {
        PARTNER_TYPE: "",
        IS_IN: "",
        IS_OUT: "",
        IS_ENABLE: "",
        PARTNER_CODE: "",
        PARTNER_SHORT_NAME: ""
      },
      // 控制打印弹窗
      isShow: false,
      rowData: {}
    };
  },
  methods: {
    printNum(row) {
      this.rowData = row;
      this.isShow = true;
    },
    detailClose() {
      this.isShow = false;
    },
    querySelect() {
      request({
        url: "/mp/wm/vl/basedata/boxcommon/queryPartnerTypeBox.do",
        method: "post",
        data: {}
      }).then(res => {
        res.listRow.map(item => {
          this.tableComponents[0].options.push({
            label: item.PARTNER_TYPE_NAME,
            value: item.PARTNER_TYPE
          });
        });
      });
    },
    downloadTemplate() {
      let downloadElement = document.createElement("a");
      let href = "/wms/fileTemplate/合作伙伴导入模板.xlsx"; // 创建下载的链接
      downloadElement.href = href;
      document.body.appendChild(downloadElement);
      downloadElement.click(); // 点击下载
      document.body.removeChild(downloadElement); // 下载完成移除元素
    },
    export() {
      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      request({
        url: "/mp/wm/vl/basedata/partner/exportPartner.do",
        method: "post",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "合作伙伴导出.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch(err => {
          exportLoading.close();
          console.log(err);
        });
    },
    //文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    handFileListChange(file, fileList) {},
    // 上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。
    beforeAvatarUpload(file) {
      if (file.size > 50 * 1024 * 1024) {
        this.$message({
          message: "文件大小不能超过50M",
          type: "warning"
        });
        return false;
      }
      let fileType = file.name.substr(file.name.lastIndexOf(".") + 1);
      if (fileType != "xls" && fileType != "xlsx") {
        this.$message({
          message: "只能导入后缀为xls,xlsx的文件！",
          type: "warning"
        });
        return false;
      }
      return true;
    },
    handleChange(param) {
      const loading = this.$loading({
        lock: true,
        text: "导入中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      const that = this;
      const url = "/mp/wm/vl/basedata/partner/importExcelToPartner.do";
      that
        .$requestUpload(url, param)
        .then(response => {
          loading.close();
          if (response.result === "1") {
            this.$message({
              message: "导入成功",
              type: "success"
            });
            that.queryTable();
          } else {
            this.$message({
              message: response.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {});
    }
  }
};
</script>
